package com.roncoo.education.course.service.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcloud.cos.utils.DateUtils;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.roncoo.education.course.common.bo.CourseLisBO;
import com.roncoo.education.course.common.bo.CourseListByTagAndCategoryBO;
import com.roncoo.education.course.common.bo.TencentcloudBO;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseChapterPeriod;
import com.roncoo.education.course.service.dao.impl.mapper.pojo.CourseChapterPeriodPojo;
import com.roncoo.education.course.service.dao.service.ICourseChapterPeriodPojoService;
import com.roncoo.education.course.util.DateUtil;
import com.roncoo.education.util.base.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/tencentcloudController")
@Api(tags = "腾讯云")
public class TencentcloudController {
	
	private final static Logger log = Logger.getLogger(TencentcloudController.class);
	
	private final static String SECRET_ID="AKIDFGyRdc8steCWzB6mBKEjrj6E6j5CfMM7";

	private final static String SECRET_KEY="e3WigX99tPhlailZ7PRJyPedLo56tRmw";
		
	@Autowired
	private ICourseChapterPeriodPojoService iCourseChapterPeriodPojoService;
		/**
		 * 上传视频到腾讯云
		 * @param courseId
		 * @param url
		 * @return
		 */
		@PostMapping("/VodUpload")
		@ApiOperation("腾讯云视频上传")
		public Result<String> VodUpload(@RequestBody TencentcloudBO tencentcloudBO){
			VodUploadClient client = new VodUploadClient(SECRET_ID, SECRET_KEY);
			VodUploadRequest request = new VodUploadRequest();
			request.setMediaFilePath(tencentcloudBO.getUrl());
			try {
			    VodUploadResponse response = client.upload("ap-guangzhou", request);
			    //获取腾讯云地址
			    String mediaUrl = response.getMediaUrl();
			    //存入课时表
			    CourseChapterPeriodPojo courseChapterPeriodPojo = new CourseChapterPeriodPojo();
			    courseChapterPeriodPojo.setVideoUrl(mediaUrl);
			    courseChapterPeriodPojo.setGmtModified(DateUtil.getDateTime(new Date()));
			    courseChapterPeriodPojo.setPeriodName(tencentcloudBO.getPeriodName());
			    courseChapterPeriodPojo.setIsExam(tencentcloudBO.getIsExam());
			    iCourseChapterPeriodPojoService.save(courseChapterPeriodPojo);
			    System.out.println(mediaUrl);
			    System.out.println(response.getFileId());
			    log.info("视频上传成功");
			} catch (Exception e) {
			    // 业务方进行异常处理
				System.out.println(e.toString());
				return Result.error("视屏上传异常");
			} 
			return Result.success("视频上传成功");
		}
		
		/**
		 * 获取课程详情
		 * @param CourseId
		 * @return
		 */
		@ApiOperation("获取课程详情")
		@PostMapping("/getCourseList")
		public Result<ArrayList<CourseChapterPeriodPojo>> getCourseList(@RequestParam("courseId")String courseId){
			List<CourseChapterPeriodPojo> list = iCourseChapterPeriodPojoService.list(new QueryWrapper<CourseChapterPeriodPojo>().eq("course_id", courseId));
			return Result.success(new ArrayList<CourseChapterPeriodPojo>(list));
		}
}
